package com.potenza.training.dictionary.server.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.potenza.training.dictionary.dao.HopePositionDao;
import com.potenza.training.dictionary.entity.HopePosition;
import com.potenza.training.dictionary.server.HopePositionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;


@Service("hopePositionServiceImpl")
public class HopePositionServiceImpl extends ServiceImpl<HopePositionDao, HopePosition> implements HopePositionService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private HopePositionDao hopePositionDao;

    @Override
    public List<HopePosition> findAll() {
        Object hopePosition = redisTemplate.opsForValue().get("hopePosition");
        if (hopePosition==null){
            List<HopePosition> list = this.list();
            redisTemplate.opsForValue().set("hopePosition", list);
            return list;
        }

        return (List<HopePosition>)hopePosition;
    }

    @Override
    public List<HopePosition> findByPid(Integer pid) {
        QueryWrapper<HopePosition> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(HopePosition::getPid,pid);
        return hopePositionDao.selectList(queryWrapper);
    }

    @Override
    public HopePosition findById(Integer id) {
        return hopePositionDao.selectById(id);
    }

    @Override
    public void saveHopePosition(HopePosition hopePosition) {
        redisTemplate.delete("hopePosition");
        this.save(hopePosition);
    }

    @Override
    public void updateHopePosition(HopePosition hopePosition) {
        redisTemplate.delete("hopePosition");
        this.updateById(hopePosition);
    }
}